package com.itjason.utils;

import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * @Author:JasonLong
 * @Date:2023/5/23
 * @Time:21:41
 */
public class UploadToLocalUtils {
    private static final String regionLocalUrl = "/region/";

    private static final String nationLocalUrl = "/nation/";

    private static final String baseUrl = "/src/main/resource/static/audio";

    //这里的ip根据实际的来配置
    private static final String ip = "http://localhost:8089/audio";

    public static String regionFileUpload(MultipartFile file,String newFileName) {
        //获取项目绝对路径
        String path = System.getProperty("user.dir");
        //替换转义符号
        path = path.replaceAll("\\\\", "/");
        //文件目录绝对路径
        String realPath = path + baseUrl + regionLocalUrl;
        try {
            file.transferTo(new File(realPath,newFileName));
            File file1 = new File(path + baseUrl);
            System.out.println("本地上传测试成功！");
        } catch (IOException e) {
            System.out.println("本地上传测试失败！");
            throw new RuntimeException(e);
        }
//        System.out.println(realPath);
        return ip + regionLocalUrl+newFileName;
    }

    public static String nationFileUpload(MultipartFile file,String newFileName) {
        String path = System.getProperty("user.dir");
        path = path.replaceAll("\\\\", "/");
        String realPath = path + baseUrl + nationLocalUrl;
        return ip + nationLocalUrl+newFileName;
    }
}
